John Li's Blog Site

Gitlab CI Pipeline Trigger

April 20, 2019

Triggering pipelines through the API

You can add a new trigger by going to your project's Settings ➔ CI/CD under Triggers. The Add trigger button will create a new token which you can then use to trigger a rerun of this particular project's pipeline. Every new trigger you create, gets assigned a different token which you can then use inside your scripts or .gitlab-ci.yml. You also have a nice overview of the time the triggers were last used.

Taking ownership of a trigger

Each created trigger when run will impersonate their associated user including
their access to projects and their project permissions.
You can take ownership of existing triggers by clicking Take ownership.
From now on the trigger will be run as you.

Making use of trigger variables

You can pass any number of arbitrary variables in the trigger API call and they
will be available in GitLab CI so that they can be used in your .gitlab-ci.yml
file. 

For example:

Consider the following .gitlab-ci.yml where we set three stages and the uploadpackage job is run only when all jobs from the test and build stages pass. When the UPLOADTO_S3 variable is non-zero, make upload is run.

stages:
- test
- build
- package

run_tests:
  stage: test
  script:
  - make test

build_package:
  stage: build
  script:
  - make build

upload_package:
  stage: package
  script:
  - if [ -n "${UPLOAD_TO_S3}" ]; then make upload; fi

You can then trigger a rebuild while you pass the UPLOADTOS3 variable and the script of the upload_package job will run:

curl --request POST \
  --form token=TOKEN \
  --form ref=master \
  --form "variables[UPLOAD_TO_S3]=true" \
  https://gitlab.example.com/api/v4/projects/9/trigger/pipeline

or via http query string: ???

You can also supply variables too when run pipeline manually pipeline variables

And more information about GitLab Webhooks

Another GitLab premium feature is Multi-project pipelines


John Li

John Li's stuff for work, study and social activities

You can follow him on Twitter